/*
 view.go
 Created by hupfei on 2019-09-11
*/

package service

import (
	"encoding/json"
	"strconv"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/drive/redis"
	"zimuzu_cvn_web_api/pkg/e"
)

type AmountChangedResponse struct {
	RechargeCvnt      float64 `json:"rechargeCvnt"`      //昨日充值数量
	WithdrawCvnt      float64 `json:"withdrawCvnt"`      //昨日提现数量
	Cvnt              float64 `json:"cvnt"`              //全网总代币数量
	UnWithdrawCvnt    float64 `json:"unWithdrawCvnt"`    //未提现总代币数量
	UnavailableCvnt   float64 `json:"unavailableCvnt"`   //不可用总代币数量
	TransferUserCount int64   `json:"transferUserCount"` //大额转账人数（单笔超过50万用户）
	WithdrawUserCount int64   `json:"withdrawUserCount"` //大额提现人数（单笔超过50万用户）
}

func AmountChanged() (data AmountChangedResponse, err error) {
	//昨日充值数量
	data.RechargeCvnt, err = model.GetYesterdayCvntChanged(1)
	if err != nil {
		return
	}

	//昨日提现数量
	data.WithdrawCvnt, err = model.GetYesterdayCvntChanged(2)
	if err != nil {
		return
	}

	//全网总代币数量
	data.Cvnt, err = GetGlobalCvnTotal()
	if err != nil {
		return
	}

	//未提现总代币数量
	data.UnWithdrawCvnt, err = model.GetUnWithdrawCvnt(1)
	if err != nil {
		return
	}

	//不可用总代币数量
	data.UnavailableCvnt, err = model.GetUnWithdrawCvnt(2)
	if err != nil {
		return
	}

	//大额转账人数
	data.TransferUserCount, err = model.GetLargeCvntChangedUserCount(1)
	if err != nil {
		return
	}

	//大额提现人数
	data.WithdrawUserCount, err = model.GetLargeCvntChangedUserCount(2)
	if err != nil {
		return
	}

	return data, nil
}

type PledgeDataResponse struct {
	TodayPledgeRouter           float64 `json:"todayPledgeRouter"`
	TodayPledgePC               float64 `json:"todayPledgePC"`
	TodayPledgeLinux            float64 `json:"todayPledgeLinux"`
	TodayPledgeRedemptionRouter float64 `json:"todayPledgeRedemptionRouter"`
	TodayPledgeRedemptionPC     float64 `json:"todayPledgeRedemptionPC"`
	TodayPledgeRedemptionLinux  float64 `json:"todayPledgeRedemptionLinux"`
	PledgeRouter                float64 `json:"pledgeRouter"`
	PledgePC                    float64 `json:"pledgePC"`
	PledgeLinux                 float64 `json:"pledgeLinux"`
}

func PledgeData() (data PledgeDataResponse, err error) {
	totalRe, err := model.GetGlobalUserCvn()
	if err != nil {
		return
	}

	data.PledgeRouter = totalRe.RouterPledgingCvn
	data.PledgePC = totalRe.PCPledgingCvn
	data.PledgeLinux = totalRe.PledgingCvn

	data.TodayPledgeRouter, err = model.GetLogTodayCvntCount(model.LogSourcePledgeRouter)
	if err != nil {
		return
	}

	data.TodayPledgePC, err = model.GetLogTodayCvntCount(model.LogSourcePledgePC)
	if err != nil {
		return
	}

	data.TodayPledgeLinux, err = model.GetLogTodayCvntCount(model.LogSourcePledge)
	if err != nil {
		return
	}

	data.TodayPledgeRedemptionRouter, err = model.GetTodayDrawCvntCount(model.PledgeRouter)
	if err != nil {
		return
	}

	data.TodayPledgeRedemptionPC, err = model.GetTodayDrawCvntCount(model.PledgePC)
	if err != nil {
		return
	}

	data.TodayPledgeRedemptionLinux, err = model.GetTodayDrawCvntCount(model.PledgeLinux)
	if err != nil {
		return
	}

	return
}

// 获取用户的数据信息
func ViewUserData() (data model.ViewUserResponse, err error) {

	cacheKey := e.BoardPrefix + "view:user:data"
	if redis.Exists(cacheKey) {
		reData, err := redis.Get(cacheKey)
		if err != nil {
			return data, err
		}
		err = json.Unmarshal([]byte(reData), &data)
		if err != nil {
			return data, err
		}
		return data, nil
	}

	data, err = model.GetUserViewData()
	if err != nil {
		return data, err
	}

	dataStr, _ := json.Marshal(data)
	redis.SetUnRe(cacheKey, string(dataStr), e.TimeFormatMin*10)
	return data, nil
}

type NodeInfoResponse struct {
	Cvnt  float64 `json:"cvnt"`
	Linux struct {
		Cvnt      float64 `json:"cvnt"`
		CvntTotal float64 `json:"cvntTotal"`
		Node      int64   `json:"node"`
		NodeTotal int64   `json:"nodeTotal"`
	} `json:"linux"`
	Pc struct {
		Cvnt      float64 `json:"cvnt"`
		CvntTotal float64 `json:"cvntTotal"`
		Node      int64   `json:"node"`
		NodeTotal int64   `json:"nodeTotal"`
	} `json:"pc"`
	Router struct {
		Cvnt      float64 `json:"cvnt"`
		CvntTotal float64 `json:"cvntTotal"`
		Node      int64   `json:"node"`
		NodeTotal int64   `json:"nodeTotal"`
	} `json:"router"`
}

//获取节点信息汇总
func NodeInfo() (info NodeInfoResponse, err error) {
	//矿池余额
	key := e.BoardPrefix + "view:cvnt"
	if redis.Exists(key) {
		cvnt, err := redis.Get(key)
		if err != nil {
			return info, err
		}
		info.Cvnt, _ = strconv.ParseFloat(cvnt, 64)
	}

	//pc
	info.Pc.Node, err = model.GetTodayNewNode(model.NodeTypePc, 1)
	if err != nil {
		return
	}
	info.Pc.NodeTotal, err = model.GetTodayNewNode(model.NodeTypePc, 2)
	if err != nil {
		return
	}
	info.Pc.Cvnt, err = model.GetCvntInfo(model.NodeTypePc, 1)
	if err != nil {
		return
	}
	info.Pc.CvntTotal, err = model.GetCvntInfo(model.NodeTypePc, 2)
	if err != nil {
		return
	}

	//linux
	info.Linux.Node, err = model.GetTodayNewNode(model.NodeTypeLinux, 1)
	if err != nil {
		return
	}
	info.Linux.NodeTotal, err = model.GetTodayNewNode(model.NodeTypeLinux, 2)
	if err != nil {
		return
	}
	info.Linux.Cvnt, err = model.GetCvntInfo(model.NodeTypeLinux, 1)
	if err != nil {
		return
	}
	info.Linux.CvntTotal, err = model.GetCvntInfo(model.NodeTypeLinux, 2)
	if err != nil {
		return
	}

	//router
	info.Router.Node, err = model.GetTodayNewNode(model.NodeTypeRouter, 1)
	if err != nil {
		return
	}
	info.Router.NodeTotal, err = model.GetTodayNewNode(model.NodeTypeRouter, 2)
	if err != nil {
		return
	}
	info.Router.Cvnt, err = model.GetCvntInfo(model.NodeTypeRouter, 1)
	if err != nil {
		return
	}
	info.Router.CvntTotal, err = model.GetCvntInfo(model.NodeTypeRouter, 2)
	if err != nil {
		return
	}
	return info, nil
}
